package com.android.bonn.fast.widget.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.StringRes;
import androidx.core.view.NestedScrollingParentHelper;
import androidx.core.view.ViewCompat;

import com.android.bonn.fast.widget.R;


/**
 * author huangyunlin@snqu.com
 * date 2018/12/13
 * description
 */
public class StateToolBarConstraintLayout extends StateConstraintLayout {

    private boolean mEnableToolbar;
    private boolean mEnableStatusBar;
    private View mStatusBarView;
    private int menuResourceId;
    private float centerTitleTextSize;
    private CharSequence customTitle;
    private int mToolbarHeight;
    private Drawable backgroundDrawable;
    private int titleTextColor;
    private CharSequence title;
    private Drawable navIcon;
    private QopToolBar mToolbar;
    private View customToolBar;

    private int mCustomToolbarViewResId;


    private NestedScrollingParentHelper mParentHelper;

    public StateToolBarConstraintLayout(Context context) {
        super(context);
        init(null, 0);
    }

    public StateToolBarConstraintLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }

    public StateToolBarConstraintLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs, defStyleAttr);
    }

    private float pixelsToSp(Context context, float px) {
        float scaledDensity = context.getResources().getDisplayMetrics().scaledDensity;
        return px / scaledDensity;
    }

    private void init(AttributeSet attrs, int defStyleAttr) {
        //LayoutInflater.from(getContext()).inflate(R.layout.decor_child_view, this, true);
        mParentHelper = new NestedScrollingParentHelper(this);
        TypedArray a = this.getContext().obtainStyledAttributes(attrs, R.styleable.StateToolBarConstraintLayout, defStyleAttr, R.style.StateViewStyle);

        mEnableToolbar = a.getBoolean(R.styleable.StateToolBarConstraintLayout_ctb_enableToolBar, true);
        mEnableStatusBar = a.getBoolean(R.styleable.StateToolBarConstraintLayout_ctb_enableStatusBar, false);
        mCustomToolbarViewResId = a.getResourceId(R.styleable.StateToolBarConstraintLayout_ctb_toolbar_layout, 0);

        menuResourceId = a.getResourceId(R.styleable.StateToolBarConstraintLayout_ctb_qop_menus, 0);
        centerTitleTextSize = a.getDimension(R.styleable.StateToolBarConstraintLayout_ctb_center_title_size, 0);
        customTitle = a.getText(R.styleable.StateToolBarConstraintLayout_ctb_center_title);
        mToolbarHeight = a.getDimensionPixelSize(R.styleable.StateToolBarConstraintLayout_ctb_toolBarHeight, android.R.attr.actionBarSize);
        backgroundDrawable = a.getDrawable(R.styleable.StateToolBarConstraintLayout_ctb_toolBarBackground);
        titleTextColor = a.getColor(R.styleable.StateToolBarConstraintLayout_ctb_titleTextColor, -1);
        title = a.getText(R.styleable.StateToolBarConstraintLayout_ctb_navigationTitle);
        navIcon = a.getDrawable(R.styleable.StateToolBarConstraintLayout_ctb_navigationIcon);

        if (mEnableStatusBar) {
            mStatusBarView = new View(getContext());
            mStatusBarView.setId(R.id.ctb_top_status_bar);
            mStatusBarView.setBackground(backgroundDrawable);
        }

        if (mEnableToolbar && mCustomToolbarViewResId == 0) {
            mToolbar = new QopToolBar(getContext());
            mToolbar.setId(R.id.ctb_all_toolbar);
        }

        a.recycle();

        if (mEnableStatusBar) {
            ViewCompat.setOnApplyWindowInsetsListener(this, (v, insets) -> {
                ViewCompat.setOnApplyWindowInsetsListener(this, null);
                addView(mStatusBarView, createStatusBarViewParams(insets.getSystemWindowInsetTop()));
                setUpToolbar();
                return insets;
            });

        } else {
            setUpToolbar();
        }


    }


    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }

    private void setUpToolbar() {


        if (mEnableToolbar) {
            LayoutParams toolbarParams = createToolbarParams();


            if (mCustomToolbarViewResId != 0) {
                customToolBar = LayoutInflater.from(getContext()).inflate(mCustomToolbarViewResId, mToolbar, false);
                /*mToolbar.setContentInsetsAbsolute(0, 0);
                mToolbar.setContentInsetsRelative(0, 0);
                mToolbar.setContentInsetStartWithNavigation(0);
                mToolbar.addView(customToolBar);*/
                customToolBar.setId(R.id.ctb_all_toolbar);
                setToolbarBackground(backgroundDrawable);
                addView(customToolBar, toolbarParams);
                return;
            }

            setToolbarBackground(backgroundDrawable);
            this.setTitleTextColor(titleTextColor);

            if (menuResourceId > 0) {
                mToolbar.setMenus(menuResourceId);
            }

            if (centerTitleTextSize > 0) {
                mToolbar.setCustomTextSize(pixelsToSp(getContext(), centerTitleTextSize));
            }


            if (!TextUtils.isEmpty(customTitle)) {
                mToolbar.setCustomTitleVisibility(View.VISIBLE);
                mToolbar.setCustomTitle(customTitle);
            } else {
                mToolbar.setCustomTitleVisibility(View.GONE);
            }

            if (!TextUtils.isEmpty(title)) {
                this.setTitle(title);
            }

            if (navIcon != null) {
                this.setNavigationIcon(navIcon);
            }

            addView(mToolbar, toolbarParams);

        }
    }

    public View getCustomToolBar() {
        return customToolBar;
    }

    private LayoutParams createStatusBarViewParams(int height) {

        LayoutParams layoutParams = new LayoutParams(0, height);
        layoutParams.topToTop = LayoutParams.PARENT_ID;
        layoutParams.startToStart = LayoutParams.PARENT_ID;
        layoutParams.endToEnd = LayoutParams.PARENT_ID;
        return layoutParams;
    }

    private LayoutParams createToolbarParams() {
        LayoutParams layoutParams = new LayoutParams(0, mToolbarHeight);

        if (mStatusBarView != null) {
            layoutParams.topToBottom = R.id.ctb_top_status_bar;
        } else {
            layoutParams.topToTop = LayoutParams.PARENT_ID;
        }

        layoutParams.startToStart = LayoutParams.PARENT_ID;
        layoutParams.endToEnd = LayoutParams.PARENT_ID;
        return layoutParams;
    }

    public QopToolBar getToolbar() {
        return mToolbar;
    }

    private void setTitle(CharSequence title) {
        if (mToolbar != null) {
            mToolbar.setTitle(title);
        }
    }

    public void setToolbarBackground(int backgroundColor) {

        setToolbarBackground(new ColorDrawable(backgroundColor));

        /*if (mToolbar != null) {
            mToolbar.setBackgroundColor(backgroundColor);
        }

        if (mStatusBarView != null) {
            mStatusBarView.setBackgroundColor(backgroundColor);
        }

        if (customToolBar != null) {
            customToolBar.setBackgroundColor(backgroundColor);
        }*/

    }

    public void setToolbarBackground(Drawable background) {

        if (customToolBar != null) {
            customToolBar.setBackground(background);
        }

        if (mToolbar != null) {
            mToolbar.setBackground(background);
        }

        if (mStatusBarView != null) {
            mStatusBarView.setBackground(background);
        }
    }

    private void setTitle(@StringRes int resId) {
        if (mToolbar != null)
            mToolbar.setTitle(resId);
    }

    private void setNavigationIcon(Drawable navIcon) {
        if (mToolbar != null)
            mToolbar.setNavigationIcon(navIcon);
    }

    public void setNavigationIcon(@DrawableRes int resId) {
        if (mToolbar != null)
            mToolbar.setNavigationIcon(resId);
    }

    public void setTitleTextColor(@ColorInt int color) {
        if (mToolbar != null) {
            mToolbar.setTitleTextColor(color);
            mToolbar.setCustomTextColor(color);
        }
    }

    public void setLoadingStateAndMenuVisible(boolean visible) {

        if (mToolbar == null) {
            return;
        }

        for (int i = 0; i < mToolbar.getMenu().size(); i++) {
            MenuItem item = mToolbar.getMenu().getItem(i);
            item.setVisible(visible);
        }
    }

    private View checkToolbar() {
        View toolbar = getToolbar();
        return toolbar != null ? toolbar : getCustomToolBar();
    }

    public void showDefaultView() {
        if (checkToolbar() != null) {
            super.showDefaultView(checkToolbar());
        }
    }

    public void showErrorView() {
        if (checkToolbar() != null) {
            super.showErrorView(checkToolbar());
        }
    }

    public void showLoadingView() {
        if (checkToolbar() != null) {
            super.showLoadingView(checkToolbar());
        }
    }


}
